const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    order: null,
    getGoodsTotalPrice: function() {
      return this.data.order.total_price;
    },
    num: '',
    showModel: false,
    unit: '',
  },

  onLoad(options) {
    app.pageOnLoad(this);
    let that = this;
    let order_no;
    let scene = decodeURIComponent(options.scene);
    if (scene && scene !== 'undefined') {
      let scene_obj = scene.split(',');
      order_no = scene_obj[0];
      app.ws_micro_shop_globalData.shopid = scene_obj[1];
      this.__shopid = scene_obj[1];
    }
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    
    app.ws_micro_shoplogin().then(res => {
      that.init(order_no);
    });
    
  },
  init(order_no) {
    let that = this;
    wx.showLoading({
      title: '正在加载',
    });
    app.ws_micro_shoprequest({
      url: api.order.clerk_detail,
      data: {
        order_no,
      },
      success: function(res) {
        if (res.code === 0) {
          let resp = res.data;
          let form_list = res.data.form_list;
          let record = resp.verify;
          let record_num = 0;
          if (record && record.length > 0) {
            for (let i in record) {
              if (Object.prototype.hasOwnProperty.call(record, i)) {
                record_num += Number(record[i].num);
              }
            }
          }
          resp.verify_total = record_num;
          for (let i in form_list) {
            if (Object.prototype.hasOwnProperty.call(form_list, i)) {
              let item = form_list[i];
              // 判断类型是否为上传图片,将value字符串拆分为数组
              if (item.type === 'uploadimg') {
                let valueArr = item.value.split(',');
                resp.form_list[i].valueArr = valueArr;
              }
            }
          }
          that.setData({
            order: resp,
          });
        } else {
          wx.showModal({
            title: '友情提示',
            showCancel: false,
            content: res.msg || '订单不存在',
            confirmText: '确认',
            success: function(res) {
              if (res.confirm) {
                let url = '/ws_micro_shop/pages/index/index';
                app.util.navigateTo(url, { shopid: that.__shopid });
              }
            },
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  getClerkNumber(e) {
    let num = e.detail.value.trim();
    num > 0
      ? this.setData({ num })
      : wx.showToast({
        title: '核销数量不正确',
        icon: 'none',
      });
  },

  hideModel() {
    this.setData({
      showModel: false,
      num: '',
    });
  },

  showModel() {
    this.setData({
      showModel: true,
    });
  },

  clerk() {
    let that = this;
    let { num, order } = this.data;
    if (
      Number(order.verifytype) === 1 &&
      order.num - order.verify_total < num
    ) {
      wx.showToast({
        title: '数量不能大于未核销的数量',
        icon: 'none',
      });
      return;
    }
    wx.showLoading({
      title: '正在加载',
    });
    app.ws_micro_shoprequest({
      url: api.order.clerk,
      data: {
        order_no: that.data.order.order_no,
        num: that.data.num,
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            showModel: false
          }, () => {
            wx.showModal({
              showCancel: false,
              content: '核销成功',
              confirmText: '确认',
              success: function (res) {
                if (res.confirm) {
                  app.util.navigateTo('/ws_micro_shop/pages/user/user', {}, true);
                }
              },
            });
          })
        } else {
          wx.showModal({
            title: '温馨提醒',
            showCancel: false,
            content: res.msg || '',
            confirmText: '确认',
            success: function(res) {
              if (res.confirm) {
                app.util.navigateTo('/ws_micro_shop/pages/index/index', {}, true);
              }
            },
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },
});
